package com.ruoyi.web.controller.store;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.RestaurantStoreResource;
import com.ruoyi.common.core.domain.entity.RestaurantStoreResourceMove;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.mapper.RestaurantStoreResourceMapper;
import com.ruoyi.system.service.IRestaurantStoreResourceMoveService;
import com.ruoyi.system.service.RestaurantStoreResourceService;
import com.ruoyi.system.service.req.CreateResourceDTO;
import com.ruoyi.system.service.req.UpdateResourceDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2023/8/6
 **/
@RestController
@RequestMapping("/store/resource")
public class StoreResourceController extends BaseController {
    @Autowired
    private RestaurantStoreResourceService restaurantStoreResourceService;

    @Resource
    IRestaurantStoreResourceMoveService restaurantStoreResourceMoveService;

    @Resource
    private RestaurantStoreResourceMapper restaurantStoreResourceMapper;

    @Log(title = "食材导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RestaurantStoreResource resource) {
        List<RestaurantStoreResource> list = restaurantStoreResourceService.selectRestaurantStoreResourceList(resource);
        ExcelUtil<RestaurantStoreResource> util = new ExcelUtil<RestaurantStoreResource>(RestaurantStoreResource.class);
        util.exportExcel(response, list, "食材");
    }

    @GetMapping("/list")
    public TableDataInfo list(RestaurantStoreResource resource) {
        startPage();
        List<RestaurantStoreResource> list = restaurantStoreResourceService.selectRestaurantStoreResourceList(resource);
        return getDataTable(list);
    }

    @GetMapping("/getAll")
    public TableDataInfo listAll(RestaurantStoreResource resource) {
        List<RestaurantStoreResource> list = restaurantStoreResourceService.selectRestaurantStoreResourceList(resource);
        return getDataTable(list);
    }

    @GetMapping("/latest")
    public TableDataInfo latest(RestaurantStoreResource resource) {
        List<RestaurantStoreResource> list = restaurantStoreResourceMapper.selectLatestResourceList(resource);
        return getDataTable(list);
    }

    //("@ss.hasPermi('store:resource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(restaurantStoreResourceService.selectRestaurantStoreResourceById(id));
    }

    //("@ss.hasPermi('store:resource:add')")
    @Log(title = "食材管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@Validated @RequestBody CreateResourceDTO createResourceDTO) {
        Boolean result = restaurantStoreResourceService.createRestaurantStoreResource(createResourceDTO);
        if (result) {
            return success();
        }
        return error();
    }

    //("@ss.hasPermi('store:resource:edit')")
    @Log(title = "食材管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody UpdateResourceDTO updateResourceDTO) {
        Boolean result = restaurantStoreResourceService.updateRestaurantStoreResource(updateResourceDTO);
        if (result) {
            return success();
        }
        return error();
    }

    //("@ss.hasPermi('store:resource:remove')")
    @Log(title = "食材管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        //校验
        RestaurantStoreResourceMove query = new RestaurantStoreResourceMove();
        query.setResourceId(id);
        List<RestaurantStoreResourceMove> move = restaurantStoreResourceMoveService.selectRestaurantStoreResourceMoveList(query);
        if (!CollectionUtils.isEmpty(move)) {
            throw new BaseException("该食材存在库存变更明细记录，无法删除");
        }
        return toAjax(restaurantStoreResourceService.deleteRestaurantStoreResourceById(id));
    }

}
